package cn.chiship.framework.upms.biz.user.controller;


import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.constants.CommonCacheConstants;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.framework.upms.biz.base.service.CacheService;
import cn.chiship.framework.upms.biz.user.entity.UpmsPost;
import cn.chiship.framework.upms.biz.user.entity.UpmsPostExample;
import cn.chiship.framework.upms.biz.user.pojo.dto.UpmsPostDto;
import cn.chiship.framework.upms.biz.user.service.UpmsPostService;
import cn.chiship.sdk.cache.service.RedisService;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.annotation.NoParamsSign;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.base.constants.CacheConstants;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.sdk.framework.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 工作岗位控制层
 * 2021/11/5
 *
 * @author lijian
 */
@RestController
@Authorization
@RequestMapping("/post")
@Api(tags = "工作岗位")
public class UpmsPostController extends BaseController<UpmsPost, UpmsPostExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsPostController.class);

    @Resource
    private UpmsPostService upmsPostService;
    @Resource
    private CacheService cacheService;
    @Resource
    private RedisService redisService;

    @Override
    public BaseService getService() {
        return upmsPostService;
    }


    @SystemOptionAnnotation(describe = "工作岗位分页")
    @ApiOperation(value = "工作岗位分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", example = "1", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", example = "10", dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", example = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query")
    })
    @Authorization
    @NoParamsSign
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(@RequestParam(required = false, defaultValue = "", value = "keyword") String keyword) {
        UpmsPostExample postExample = new UpmsPostExample();
        UpmsPostExample.Criteria criteria1 = postExample.createCriteria();
        UpmsPostExample.Criteria criteria2 = postExample.createCriteria();
        if (!StringUtil.isNull(keyword)) {
            criteria1.andCodeLike("%" + keyword + "%");
            criteria2.andNameLike("%" + keyword + "%");
        }
        postExample.or(criteria2);
        PageVo pageVo = super.page(postExample);
        cacheService.cachePosts();
        return super.responseEntity(BaseResult.ok(pageVo));
    }

    @SystemOptionAnnotation(describe = "保存工作岗位数据", option = BusinessTypeEnum.SYSTEM_OPTION_SAVE)
    @ApiOperation(value = "保存工作岗位")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid UpmsPostDto upmsPostDto) {
        UpmsPost upmsPost = new UpmsPost();
        BeanUtils.copyProperties(upmsPostDto, upmsPost);
        return super.responseEntity(super.save(upmsPost));
    }


    /**
     * 更新工作岗位
     */
    @SystemOptionAnnotation(describe = "根据主键更新工作岗位数据", option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE)
    @ApiOperation(value = "更新工作岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工作岗位id", required = true, dataTypeClass = String.class, paramType = "path")
    })
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid UpmsPostDto upmsPostDto) {
        UpmsPost upmsPost = new UpmsPost();
        BeanUtils.copyProperties(upmsPostDto, upmsPost);
        return super.responseEntity(super.update(id, upmsPost));
    }

    @SystemOptionAnnotation(describe = "根据工作岗位主键集合删除", option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE)
    @ApiOperation(value = "删除工作岗位数据")
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody @Valid List<String> ids) {
        UpmsPostExample postExample = new UpmsPostExample();
        postExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(postExample));

    }

    @SystemOptionAnnotation(describe = "获取工作岗位")
    @ApiOperation(value = "获取工作岗位")
    @GetMapping(value = "/list")
    public ResponseEntity<BaseResult> list() {
        return super.responseEntity(BaseResult.ok(redisService.get(CommonCacheConstants.buildKey(CacheConstants.REDIS_POST_PREFIX))));
    }

    @SystemOptionAnnotation(describe = "工作岗位启用&锁定", option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE)
    @ApiOperation(value = "工作岗位启用&锁定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工作岗位id", required = true, dataTypeClass = String.class, paramType = "path"),
            @ApiImplicitParam(name = "status", value = "启用(false)&禁用(true)", required = true, dataTypeClass = Boolean.class, paramType = "body")
    })
    @PostMapping(value = "changeIsState/{id}")
    public ResponseEntity<BaseResult> changeIsState(@PathVariable("id") String id, @RequestBody Boolean status) {
        UpmsPost upmsPost = upmsPostService.selectByPrimaryKey(id);
        upmsPost.setStatus(Boolean.TRUE.equals(status) ? BaseConstants.YES : BaseConstants.NO);
        return super.responseEntity(upmsPostService.updateByPrimaryKeySelective(upmsPost));
    }

}
